package com.itany.nmms.controller;

import com.github.pagehelper.PageInfo;
import com.itany.nmms.constant.ResponseCodeConstant;
import com.itany.nmms.entity.Product;
import com.itany.nmms.entity.ProductType;
import com.itany.nmms.exception.FileUploadErrorException;
import com.itany.nmms.exception.ProductExistException;
import com.itany.nmms.exception.RequestParameterErrorException;
import com.itany.nmms.service.ProductService;
import com.itany.nmms.service.ProductTypeService;
import com.itany.nmms.util.ParameterUtil;
import com.itany.nmms.util.ResponseResult;
import com.sun.net.httpserver.Authenticator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 黄衡熙
 * @version 1.0
 * 产品详细
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductTypeService typeService;
    @Autowired
    private ProductService productService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        List<ProductType> enabledTypes = typeService.findEnabled();
        List<ProductType> types = typeService.findAll();
        mav.addObject("enabledTypes", enabledTypes);
        mav.addObject("types", types);

        PageInfo<Product> productPage = productService.findPage(pageNo, pageSize);
        mav.addObject("productPage", productPage);

        mav.setViewName("backend/productManager");
        return mav;
    }

//    @RequestMapping("/findAllProduct")
//    public ModelAndView findAllProduct(String pageNo,String pageSize){
//        ModelAndView mav=new ModelAndView();
//        PageInfo<Product> product=productService.findPage(pageNo,pageSize);
//        mav.addObject("product",product);
//        mav.setViewName("backend/productManage");
//        return mav;
//    }

    @RequestMapping("/addProduct")
    public ResponseResult addProduct(String name, String price, String description, @RequestParam("file") CommonsMultipartFile file, String typeId, HttpSession session) {
        ResponseResult result = new ResponseResult();
        try {
            productService.addProduct(name, price, description, file, typeId, session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (FileUploadErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/findById")
    public ResponseResult findById(String id) {
        ResponseResult result = new ResponseResult();

        try {
            Product product = productService.findById(id);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
            result.setReturnObject(product);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/modifyProduct")
    public ResponseResult modify(String id, String name, String price, String description, @RequestParam("file") CommonsMultipartFile file, String typeId, HttpSession session) {
        ResponseResult result = new ResponseResult();
        try {
            productService.modifyProduct(id, name, price, description, file, typeId, session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ProductExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (FileUploadErrorException e){
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/deleteById")
    public ResponseResult deleteById(String id,HttpSession session){
        ResponseResult result=new ResponseResult();

        try {
            productService.deleteById(id,session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        }catch(Exception e){
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

}
